package com.graph.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.graph.base.GraphInterface;
import com.graph.base.VertexInterface;

public class GraphFileUtils {
     private static class LazyHolder {    
        private static final GraphFileUtils INSTANCE = new GraphFileUtils();    
     }    
     public static final GraphFileUtils getInstance() {    
        return LazyHolder.INSTANCE;    
     }
	
   /**
    * @Description:读取数据
    * @param path   路径
    * @param fileName   文件名称
    * @return  图对象
    * @author: wujiang
    * @time:2017-1-6 下午3:59:03
    */
   public GraphInterface read(String path, String fileName, GraphInterface graph){
	   ArrayList<String> graphData = null;
	   File file = new File(path + fileName);
		if (file.exists() && file.isFile()) {
			try {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
				BufferedReader bufferreader = new BufferedReader(read);
				String lineTxt = null;
				graphData = new ArrayList<String>();
				try {
					while ((lineTxt = bufferreader.readLine()) != null) {
						graphData.add(lineTxt);
					}
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("读取数据错误");
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				System.out.println("读取数据错误");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.out.println("读取数据错误");
			}
		}
		if(graphData != null){
		String[] data = graphData.get(0).split(",");
		for (String string2 : data) {
			graph.addVertex(string2);
		}
		graph.initStandAnswer(graphData.get(1).split(";"));
		for (int i = 1; i < graphData.size(); i++) {
			String[] data2 = graphData.get(i).split(",");
			String vertexName = data2[0];// 节点名称
			graph.addEdge(vertexName, data2[1], 0);
			graph.addEdge(data2[1],vertexName, 0);
		}
		}else{
			System.out.println("数据格式不正确！");
		}
	   return graph;
   }
   
   
   
   /**
    * @Description:读取数据
    * @param path   路径
    * @param fileName   文件名称
    * @return  图对象
    * @author: wujiang
    * @time:2017-1-6 下午3:59:03
    */
   public GraphInterface read1(String path,String fileName,GraphInterface graph){
	   ArrayList<String> graphData = null;
	   File file = new File(path+fileName);
		if (file.exists() && file.isFile()) {
			try {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
				BufferedReader bufferreader = new BufferedReader(read);
				String lineTxt = null;
				graphData = new ArrayList<String>();
				try {
					while ((lineTxt = bufferreader.readLine()) != null) {
						graphData.add(lineTxt);
					}
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("读取数据错误");
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				System.out.println("读取数据错误");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.out.println("读取数据错误");
			}
		}
		if(graphData!=null){
		String[] data = graphData.get(0).split(",");
		for (String string2 : data) {
			graph.addVertex(string2);
		}
		for (int i = 1; i < graphData.size(); i++) {
			String[] data2 = graphData.get(i).split(",");
			String vertexName = data2[0];// 节点名称
			for (int j = 1; j< data2.length; j++) {
				String string = data2[j];
				graph.addEdge(vertexName, string, 0);
				graph.addEdge(string, vertexName, 0);
			}
		}
		}else{
			System.out.println("数据格式不正确！");
		}
	   return graph;
   }
   
   
   
   
   
	 private int getMiddle(List<VertexInterface> result, int low, int high) {
		 VertexInterface tmp1 = result.get(low);
		 Integer tmp = tmp1.getDegree();    //数组的第一个作为中轴
         while (low < high) {     
             while (low < high && result.get(high).getDegree()<= tmp) {     
         	 	high--;
             }     
             result.set(low, result.get(high));   //比中轴小的记录移到低端     
             while (low < high && result.get(low).getDegree() >= tmp) {     
                 low++;     
             }     
             result.set(high, result.get(low));   //比中轴大的记录移到高端
         }     
         result.set(low, tmp1);              //中轴记录到尾
         return low;                   //返回中轴的位置     
     }    
     private void _quickSort(List<VertexInterface> result, int low, int high) {
         if (low < high) {     
            int middle = getMiddle(result, low, high);  //将list数组进行一分为二     
             _quickSort(result, low, middle - 1);        //对低字表进行递归排序     
            _quickSort(result, middle + 1, high);       //对高字表进行递归排序     
         }     
     }   
    public void sortAndCode(List<VertexInterface> result) {
         if (result.size() > 0) {    //查看数组是否为空     
             _quickSort(result, 0, result.size() - 1);     
     }  
         //概率模体算法另外实用的一套编号
		for (int i = 0; i < result.size(); i++) {
			result.get(i).setReNumber(i+1);
		}
		//
    }   

   
}
